package com.wwwarehouse.carddesk.fragment.mytabfragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.bean.CardDeskMYPersonResponseBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.carddesk.eventbus_event.CardDeskMyEmailChangeEvent;
import com.wwwarehouse.common.activity.base.BaseApplication;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.CircleImageView;
import com.wwwarehouse.common.custom_widget.ElasticScrollView;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.rsa.StringUtils;

import java.util.HashMap;
import java.util.Map;

import de.greenrobot.event.EventBus;

/**
 * author ：minifly
 * date: 2017/10/21
 * time: 10:52
 * desc:
 */
public class MyOwnMessageFragment extends BaseFragment implements View.OnClickListener{

    private View v;
    private ImageView mDeskMyMessageComformImg;
    private TextView mDeskMyMessageComformName;
    private TextView mDeskMyMessageComformText;
    private ConstraintLayout mDeskMyMessageComformConstraintlayout;
    private LinearLayout mDeskMyMessageComformUsernameemailLin;
    private LinearLayout mDeskMyMessageComformHeadlin;
    private TextView mDeskMyMessageComformPhoneTxt;
    private TextView mDeskMyMessageComformEmailTxt;
    private LinearLayout mDeskMyMessageComformPhoneemailLin;
    private TextView mDeskMyMessageComformFingerTxt;
    private LinearLayout mDeskMyMessageComformFingerLin;
    private LinearLayout mDeskMyMessageComformResourceLin;
    private ElasticScrollView mElasticScroll;
    private TextView mDeskMyResourceTxt;
    private CircleImageView headImageview;
    private StateLayout mStateLayout;

    private static int REQUEST_OPERATION_FAVORITE  = 10003;
    private CardDeskMYPersonResponseBean bean;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.fragment_desk_my_message, container, false);
        initView(v);
        EventBus.getDefault().register(this);
        return v;

    }

    public void initView(View view) {

        mDeskMyMessageComformImg = (ImageView) v.findViewById(R.id.desk_my_message_comform_img);
        mDeskMyMessageComformName = (TextView) v.findViewById(R.id.desk_my_message_comform_name);
        mDeskMyMessageComformText = (TextView) v.findViewById(R.id.desk_my_message_comform_text);
        mDeskMyMessageComformConstraintlayout = (ConstraintLayout) v.findViewById(R.id.desk_my_message_comform_constraintlayout);
        mDeskMyMessageComformUsernameemailLin = (LinearLayout) v.findViewById(R.id.desk_my_message_comform_usernameemail_lin);
        mDeskMyMessageComformHeadlin = (LinearLayout) v.findViewById(R.id.desk_my_message_comform_headlin);
        mDeskMyMessageComformPhoneTxt = (TextView) v.findViewById(R.id.desk_my_message_comform_phone_txt);
        mDeskMyMessageComformEmailTxt = (TextView) v.findViewById(R.id.desk_my_message_comform_email_txt);
        mDeskMyMessageComformPhoneemailLin = (LinearLayout) v.findViewById(R.id.desk_my_message_comform_phoneemail_lin);
        mDeskMyMessageComformFingerTxt = (TextView) v.findViewById(R.id.desk_my_message_comform_finger_txt);
        mDeskMyMessageComformFingerLin = (LinearLayout) v.findViewById(R.id.desk_my_message_comform_finger_lin);
        mDeskMyMessageComformResourceLin = (LinearLayout) v.findViewById(R.id.desk_my_message_comform_resource_lin);
        mElasticScroll = (ElasticScrollView) v.findViewById(R.id.elastic_scroll);
        mDeskMyResourceTxt = (TextView) v.findViewById(R.id.desk_my_message_resource_txt);
        headImageview = (CircleImageView) v.findViewById(R.id.taskdesk_my_message_head);
        mStateLayout = (StateLayout) v.findViewById(R.id.desk_my_message_statelayout);

        mDeskMyMessageComformUsernameemailLin.setOnClickListener(this);
        mDeskMyMessageComformPhoneemailLin.setOnClickListener(this);
        mStateLayout.showProgressView(true);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof MyOwnMessageFragment) {
            mActivity.setTitle(getString(R.string.taskdesk_my_mymessage));
        }
    }

    /**
     * 更新页面信息
     */
    private void updatePage(){
        //获取姓名，邮箱，手机号，身高，体重（没有则返回空字符串）
        mDeskMyMessageComformName.setText(bean.getPersonName()+"");

        if(StringUtils.isNullString("" + bean.getMobile())){
            mDeskMyMessageComformPhoneTxt.setText("" + getString(R.string.taskdesk_my_message_nocofirm_phone));
        }else{
            mDeskMyMessageComformPhoneTxt.setText(""+ getString(R.string.taskdesk_my_message_cofirm_phone));
        }

        if(StringUtils.isNullString("" + bean.getEmail())){
            mDeskMyMessageComformEmailTxt.setText("" + getString(R.string.taskdesk_my_message_nocofirm_email));
        }else{
            mDeskMyMessageComformEmailTxt.setText(""+ getString(R.string.taskdesk_my_message_cofirm_email));
        }

        StringBuilder stringBuilder = new StringBuilder("");
        //用户设置的身高体重需要显示在拼卡上。没有身高或体重信息时，显示“未维护身高信息  体重50kg”或“身高160cm  未维护体重信息”
        if(StringUtils.isNullString("" + bean.getWeight())){
            stringBuilder.append(getString(R.string.taskdesk_my_message_resource_weight));
        }else{
            stringBuilder.append(getString(R.string.taskdesk_my_message_resource_weight_use).replace("$",bean.getWeight()));
        }

        if(StringUtils.isNullString("" + bean.getHeight())){
            stringBuilder.append("  " + getString(R.string.taskdesk_my_message_resource_height));
        }else{
            stringBuilder.append("  " + getString(R.string.taskdesk_my_message_resource_height_use).replace("$",bean.getHeight()));
        }

        mDeskMyResourceTxt.setText("" + stringBuilder.toString());

        BaseApplication.getApplicationInstance().displayImg(bean.getFaceUrl(),headImageview);
    }

    @Override
    public void requestDatas() {
        Map map = new HashMap();
        NoHttpUtils.httpPost(CardDeskConstant.URL_MY_PERSON_MESSAGE, map, responseListener, REQUEST_OPERATION_FAVORITE);
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.desk_my_message_comform_usernameemail_lin) {
            if(bean ==null){
                return;
            }
            Bundle bundleIn = new Bundle();
            bundleIn.putString("name", bean.getPersonName());
            MyMessageChangePasswordFragment fragmentIn = new MyMessageChangePasswordFragment();
            fragmentIn.setArguments(bundleIn);
            pushFragment(fragmentIn);


        } else if (i == R.id.desk_my_message_comform_phoneemail_lin) {
            if(bean ==null){
                return;
            }

            Bundle bundle = new Bundle();
            bundle.putString("email", bean.getEmail());
            bundle.putString("phone", bean.getMobile());
            MyMessageNameEmailFragment fragment = new MyMessageNameEmailFragment();
            fragment.setArguments(bundle);
            pushFragment(fragment);

        }
    }

    NoHttpUtils.OnResponseListener responseListener = new NoHttpUtils.OnResponseListener() {
        @Override
        public void onSucceed(CommonClass commonClass, int i) {
            if("0".equals(commonClass.getCode())){
                bean = JSONObject.parseObject(commonClass.getData().toString(), CardDeskMYPersonResponseBean.class);
                updatePage();
            }else{
                mStateLayout.showSystemView(commonClass.getMsg(),true);
                mStateLayout.setSystemListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        requestDatas();
                    }
                });
            }
        }

        @Override
        public void onFailed(String s, int i) {

        }

        @Override
        public void onFinish(int i) {
            mStateLayout.showContentView();
        }
    };

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }



    public void onEventMainThread(Object event) {
        if( event instanceof CardDeskMyEmailChangeEvent){
            CardDeskMyEmailChangeEvent event1 = (CardDeskMyEmailChangeEvent)event;
            mDeskMyMessageComformEmailTxt.setText(""+ getString(R.string.taskdesk_my_message_cofirm_email));
            bean.setEmail(event1.getMsg());
        }
    }
}
